package com.polaris.live.widget.room_banner

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.os.bundleOf
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewTreeLifecycleOwner
import androidx.lifecycle.findViewTreeLifecycleOwner
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.WidgetRoomBannerLiveBinding
import com.polaris.live.net.liveService
import com.polaris.live.ui.live.widget.LuckyBagView
import com.polaris.live.ui.luckybag.fragment.dialog.LuckyBagMessageDialogFragment
import com.polaris.live.utils.fragment.FragmentUtils
import com.polaris.live.utils.fragment.showNewDialogFragment
import com.polaris.live.widget.MagicLampBtnView
import com.polaris.live.widget.RechargeActView

/**
 * banner列表
 *
 * @author Created by 半仙 on 2024/7/30/030 19:55
 */
class RoomBannerListView(context: Context, attrs: AttributeSet?) : ConstraintLayout(context, attrs) {

    private val mBinding = WidgetRoomBannerLiveBinding.inflate(LayoutInflater.from(context), this, true)

    private var mLiveId: Long? = null    //直播id
    private var mUserId: Long? = null    //主播id

    fun initBannerData(lifecycle: Lifecycle, viewModel: BaseViewModel, livdId: Long, userId: Long) {
        //如果相等表示当前你看的主播和你新刷到的主播是一个人
        if (mUserId == userId) return
        upDataId(livdId, userId)
        mLiveId?.let {
            viewModel.request({ liveService.getRoomBanner(it) }, { list ->
                mBinding.llRoomList.removeAllViews()
                list.forEach {
                    when (it.type) {
                        //普通banner
                        COMMOM -> {
                            val roomAbsBannerView = RoomAbsBannerView(context, null).apply {
                                ViewTreeLifecycleOwner.set(this, this@RoomBannerListView.findViewTreeLifecycleOwner())
                                setAbsBanner(lifecycle, it.banners)
                            }
                            mBinding.llRoomList.addView(roomAbsBannerView)
                        }
                        //炸弹红包
                        LUCKYBAG -> {
                            val luckyBagView = LuckyBagView(context).apply {
                                registerBagNum(viewModel, mUserId)
                                setOnSingleClickListener {
                                    val context = ViewUtils.getAliveTopActivity() ?: return@setOnSingleClickListener
                                    FragmentUtils.getFragmentManager(context)?.showNewDialogFragment(
                                        LuckyBagMessageDialogFragment().also {
                                            it.arguments = bundleOf("liveId" to mUserId)
                                        },
                                        LuckyBagMessageDialogFragment.FRAGMENT_TAG
                                    )
                                    clearNum()
                                }
                            }
                            mBinding.llRoomList.addView(luckyBagView)
                        }
                        //充值特惠
                        ACTRECHARGE -> {
                            val rechargeActView = RechargeActView(context, null).apply {
                                registerObserve(lifecycle, viewModel)
                            }
                            mBinding.llRoomList.addView(rechargeActView)
                        }
                        //神灯
                        MAGICLAMP -> {
                            val magicLampBtnView = MagicLampBtnView(context, null).apply {
                                registerObserve(lifecycle, viewModel, userId)
                                setOnSingleClickListener {
                                    mMagicLampListener?.invoke()
                                }
                            }
                            mBinding.llRoomList.addView(magicLampBtnView)
                        }
                    }
                }
            })
        }
    }

    fun upDataMagicLamp() {
        for (i in 0 until mBinding.llRoomList.childCount) {
            val child = mBinding.llRoomList.getChildAt(i)
            if (child is MagicLampBtnView) {
                child.upDataMagicLamp()
            }
        }
    }

    private fun upDataId(livdId: Long, userId: Long) {
        mLiveId = livdId
        mUserId = userId
    }

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mOnTouchCallback?.invoke(false)
            }

            MotionEvent.ACTION_UP -> {
                mOnTouchCallback?.invoke(true)
            }
        }
        parent.requestDisallowInterceptTouchEvent(true)
        return super.dispatchTouchEvent(event)
    }


    private var mOnTouchCallback: ((Boolean) -> Unit)? = null

    fun setOnTouchCallback(block: ((Boolean) -> Unit)) {
        mOnTouchCallback = block
    }

    private var mMagicLampListener: (() -> Unit)? = null
    fun setMagicLampListener(listener: (() -> Unit)) {
        mMagicLampListener = listener
    }

    companion object {

        const val COMMOM = 1                //普通banner
        const val LUCKYBAG = 2              //福袋
        const val ACTRECHARGE = 3           //充值特惠
        const val MAGICLAMP = 4             //神灯
    }
}